<template>
  <el-dialog :title="title" :visible.sync="visible" append-to-body width="900px">
    <div class="update-container">
      <step-line :step-list="stepList" :step-index="currentStep" @change="handleStepChange" />
      <div class="form-block">
        <template v-if="currentStep === 1">
          <BasicForm />
        </template>
        <template v-else-if="currentStep === 2">
          <BusinessForm />
        </template>
        <template v-else-if="currentStep === 3">
          <LinkageForm />
        </template>
        <template v-else-if="currentStep === 4">
          <ReportForm />
        </template>
      </div>
    </div>
    <div slot="footer" class="footer">
      <el-button size="mini" :disabled="currentStep === 1" @click="handlePrevStep">
        上一步
      </el-button>
      <template v-if="currentStep === 5">
        <el-button size="mini" type="primary">
          完成
        </el-button>
      </template>
      <template v-else>
        <el-button size="mini" type="primary" @click="handleNextStep">
          下一步
        </el-button>
      </template>
    </div>
  </el-dialog>
</template>

<script>
import StepLine from './component/StepLine'
import BasicForm from './component/formList/BasicForm/index'
import BusinessForm from './component/formList/BusinessForm/index'
import LinkageForm from './component/formList/LinkageForm/index'
import ReportForm from './component/formList/ReportForm/index'
export default {
  name: 'BusinessUpdate',
  components: { StepLine, BasicForm, BusinessForm, LinkageForm, ReportForm },
  data() {
    return {
      visible: false,
      title: '创建业务', // ### mark

      currentStep: 1
    }
  },
  computed: {
    stepList: function() {
      const { basicForm, businessForm, linkageForm, reportForm } = this.$store.state.businessFormData
      return [
        { name: basicForm.name, status: basicForm.status, errMsg: basicForm.errMsg },
        { name: businessForm.name, status: businessForm.status, errMsg: businessForm.errMsg },
        { name: linkageForm.name, status: linkageForm.status, errMsg: linkageForm.errMsg },
        { name: reportForm.name, status: reportForm.status, errMsg: reportForm.errMsg },
        { name: '完成', status: false }
      ]
    }
  },
  methods: {
    open() {
      this.$data.visible = true
    },
    handlePrevStep() {
      this.$data.currentStep -= 1
    },
    handleNextStep() {
      const currentStep = this.stepList[this.$data.currentStep - 1]
      if (!currentStep.status) {
        this.$message.warning(currentStep.errMsg[0])
        return
      }
      this.$data.currentStep += 1
    },
    handleStepChange(e, i) {
      this.$data.currentStep = i + 1
    }
  }
}
</script>

<style lang="scss" scoped>
  .update-container {
    display: flex;
  }
</style>
